﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jxym.DBUtility;
using Jxym.wlkc2011.Module.Default.model;
using System.Data.SqlClient;

namespace Jxym.wlkc2011.Module.Default.SERVER.Permissions
{
    public partial class Validation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string command = ""; if (Request.Params["command"] != null) { command = Request.Params["command"]; } if (command == "") { return; }
                int modelId = 0; if (Request.Params["modelId"] != null) { modelId = Convert.ToInt32(Jxym.Common.Request.Params("modelId")); } if (modelId == 0) { return; }
                bool validation = GetPermissionsValidation(modelId, command);
                Response.Write(validation);
            }
        }

        MsSqlDal<User_Info> iUi = new MsSqlDal<User_Info>();

        /// <summary>
        /// 权限验证
        /// </summary>
        /// <param name="command">命令 [可用命令 S|A|D|U ]</param>
        /// <param name="modelId">模块编号</param>
        /// <returns></returns>
        public bool GetPermissionsValidation(int modelId, string command)
        {
            bool validation = false;

            MsSqlDal<User_RoleConfig> iUrc = new MsSqlDal<User_RoleConfig>();

            IList<User_RoleConfig> urcList = iUrc.GetList("UserID=" + Jxym.Common.Request.AdminID("user"), null, "", 0);

            string roles = "";

            foreach (User_RoleConfig urc in urcList)
            {
                roles += " RoleID=" + urc.RoleID + " or";
            }

            roles = roles.TrimEnd("or".ToCharArray());

            MsSqlDal<User_RolePermissionConfig> iUrpc = new MsSqlDal<User_RolePermissionConfig>();

            User_RolePermissionConfig urpc = iUrpc.GetModel("(" + roles + ") AND MenuID=" + modelId, null);

            if (urpc == null) return false;

            if (urpc.PermissionValue != "" && urpc.PermissionValue.Length == 4)
            {
                validation = GetCP(command, urpc.PermissionValue);
            }

            return validation;
        }

        private bool GetCP(string command, string permissionValue)
        {
            bool validation = false;

            char[] pvs = permissionValue.ToCharArray();

            switch (command)
            {
                default://查
                    if (pvs[0] == '1') validation = true;
                    break;
                case "A"://增
                    if (pvs[1] == '1') validation = true;
                    break;
                case "U"://改
                    if (pvs[2] == '1') validation = true;
                    break;
                case "D"://删
                    if (pvs[3] == '1') validation = true;
                    break;
            }

            return validation;
        }
    }
}